package com.smile.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.smile.api.UserApi;
import com.smile.commons.page.MyBatisPlusPageConverter;
import com.smile.commons.page.R;
import com.smile.mapper.OperateLogMapper;
import com.smile.model.domain.SmilePlusOperateLog;
import com.smile.model.dto.request.AddOperateLogRequestDTO;
import com.smile.model.dto.request.GetOperateLogListRequestDTO;
import com.smile.model.response.response.GetOperateLogListResponseDTO;
import com.smile.model.request.GetUserByIdRequest;
import com.smile.model.response.GetUserByIdResponse;
import com.smile.service.OperateLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class OperateLogServiceImpl implements OperateLogService {
    @Autowired
    private OperateLogMapper operateLogMapper;

    @Autowired
    private UserApi userApi;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean addOperateLogRequest(AddOperateLogRequestDTO addOperateLogRequestDTO) {
        SmilePlusOperateLog smilePlusOperateLog = new SmilePlusOperateLog();
        BeanUtil.copyProperties(addOperateLogRequestDTO, smilePlusOperateLog);
        GetUserByIdRequest getUserByIdRequest = new GetUserByIdRequest();
        getUserByIdRequest.setId(addOperateLogRequestDTO.getUserId());
        R<GetUserByIdResponse> userById = userApi.getUserById(getUserByIdRequest);
        if (ObjectUtil.isNotEmpty(userById) && ObjectUtil.isNotNull(userById.getData())) {
            smilePlusOperateLog.setUserName(userById.getData().getUserName());
        }
        smilePlusOperateLog.setCreateTime(LocalDateTime.now());
        try{
            operateLogMapper.insert(smilePlusOperateLog);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Boolean.TRUE;
    }

    @Override
    public Page<GetOperateLogListResponseDTO> getOperateLogList(GetOperateLogListRequestDTO getOperateLogListRequestDTO) {
        QueryWrapper<SmilePlusOperateLog> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("id");
        if (ObjectUtil.isNotNull(getOperateLogListRequestDTO)) {
            if (StrUtil.isNotBlank(getOperateLogListRequestDTO.getUserName())){
                queryWrapper.eq("user_name", getOperateLogListRequestDTO.getUserName());
            }
            if (ObjectUtil.isNotNull(getOperateLogListRequestDTO.getStartTime())&&ObjectUtil.isNotNull(getOperateLogListRequestDTO.getEndTime())){
                queryWrapper.between("create_time", getOperateLogListRequestDTO.getStartTime(), getOperateLogListRequestDTO.getEndTime());
            }
        }

        Page<SmilePlusOperateLog> page = new Page<>(getOperateLogListRequestDTO.getCurrentPage(), getOperateLogListRequestDTO.getPageSize());
        Page<SmilePlusOperateLog> smilePlusOperateLogPage = operateLogMapper.selectPage(page, queryWrapper);
        Page<GetOperateLogListResponseDTO> getOperateLogListResponseDTOPage = MyBatisPlusPageConverter.convertPage(smilePlusOperateLogPage, GetOperateLogListResponseDTO.class);
        List<GetOperateLogListResponseDTO> collect = smilePlusOperateLogPage.getRecords().stream().map(i -> {
            GetOperateLogListResponseDTO getOperateLogListResponseDTO = new GetOperateLogListResponseDTO();
            BeanUtil.copyProperties(i, getOperateLogListResponseDTO);
            if (i.getOperateResult()) {
                getOperateLogListResponseDTO.setOperateResult("成功");
            } else {
                getOperateLogListResponseDTO.setOperateResult("失败");
            }
            switch (i.getOperateType()) {
                case 1:
                    getOperateLogListResponseDTO.setOperateType("新增");
                    break;
                case 2:
                    getOperateLogListResponseDTO.setOperateType("编辑");
                    break;
                case 3:
                    getOperateLogListResponseDTO.setOperateType("删除");
                    break;
                default:
                    break;
            }

            getOperateLogListResponseDTO.setOperateTime(i.getCreateTime());
            return getOperateLogListResponseDTO;
        }).collect(Collectors.toList());
        getOperateLogListResponseDTOPage.setRecords(collect);
        return getOperateLogListResponseDTOPage;
    }
}
